﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace Excel2Word
{
    public class ExcelInputHelper
    {

        public static String ABSOLUTE_TYPE = "绝对定位";
        public static String RELATIVE_TYPE = "相对定位";
        public static String NAME_TYPE = "命名定位";



        public String locateType = RELATIVE_TYPE;

        //相对位置时选择的是key还是value
        public Boolean isKey = true;
        //当前记录的对象
        SourceElement currentSE;


        private static ExcelInputHelper thisEnt = null;

        public static ExcelInputHelper GetInstance()
        {
            if (thisEnt == null)
            {
                thisEnt = new ExcelInputHelper();
            }
            return thisEnt;
        }


        public void initInput(Workbook workbook)
        {
            //监听点击事件
            workbook.SheetSelectionChange += workbook_SheetSelectionChange;

        }

        public void releaseInput(Workbook workbook)
        {
            workbook.SheetSelectionChange -= workbook_SheetSelectionChange;
        }


        public void setInputType(String type)
        {
            this.locateType = type;
        }


        void workbook_SheetSelectionChange(object Sh, Range range)
        {
            if (this.locateType.Equals(RELATIVE_TYPE))
            {
                if (isKey)
                {
                    if (range.Value == null)
                    {
                        AppStore.curOprateWindow.setTip(string.Format("字段名不能为空", currentSE.fieldName));
                        return;
                    }
                    currentSE = new SourceElement();
                    currentSE.filedType = SourceElement.FIELD_TYPE_NORMAL;
                    currentSE.fieldAddress = range.Address;
                    currentSE.fieldName = range.Value.ToString();
                    currentSE.locateType = this.locateType;
                    currentSE.sheetName = ((Worksheet)Sh).Name;
                    currentSE.range1 = range;
                    //开始输入VALUE
                    AppStore.curOprateWindow.setTip(string.Format("设置字段名{0}成功！", currentSE.fieldName));
                    range.Interior.Color = 65535;
                    isKey = false;

                }
                else
                {
                    var currentSheetName = ((Worksheet)Sh).Name;
                    if (currentSheetName.Equals(currentSE.sheetName))
                    {
                        currentSE.valueAddress = range.Address;
                        currentSE.fieldValue = range.Value == null ? "" : range.Value.ToString();
                        currentSE.range2 = range;
                        currentSE.locatePara = calRelativePosition(currentSE);
                        AppStore.listEle.Add(currentSE);
                        isKey = true;
                        range.Interior.Color = 16737996;
                        AppStore.curOprateWindow.setTip(string.Format("设置字段值{0}成功！", currentSE.fieldValue));
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("相对位置只支持一个SHEET下的单元格", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            else if (this.locateType.Equals(ABSOLUTE_TYPE))
            {
                currentSE = new SourceElement();
                currentSE.fieldName = AppStore.targetWorkbook.Application.InputBox("请输入字段名称");
                currentSE.filedType = SourceElement.FIELD_TYPE_NORMAL;
                currentSE.fieldValue = range.Value == null ? "" : range.Value.ToString();
                currentSE.valueAddress = range.Address;
                currentSE.locateType = ABSOLUTE_TYPE;
                currentSE.sheetName = range.Worksheet.Name;
                currentSE.range1 = range;
                currentSE.range2 = range;
                AppStore.listEle.Add(currentSE);
                AppStore.curOprateWindow.setTip(string.Format("设置绝对位置 {0} 成功！",currentSE.valueAddress));
            }
            else
            {
                currentSE = new SourceElement();
                currentSE.filedType = SourceElement.FIELD_TYPE_NORMAL;

                string rangeName = null;

                try
                {
                    var thisName = range.Name;
                }
                catch (Exception)
                {
                    var re = System.Windows.Forms.MessageBox.Show("未定义命名，是否添加命名?","提示！" , System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Information);
                    if (re == System.Windows.Forms.DialogResult.OK)
                    {
                        bool isOk = false;
                        while (!isOk)
                        {
                            rangeName = AppStore.targetWorkbook.Application.InputBox("请输入命名名称：");
                            for (int i = 0; i < AppStore.targetWorkbook.Names.Count; i++)
                            {
                                if (AppStore.targetWorkbook.Names.Item(i+1).Name.Equals(rangeName))
                                {
                                    System.Windows.Forms.MessageBox.Show("与其他命名重复，请重新命名！");
                                    break;
                                }
                            }
                            isOk = true;
                        }

                        AppStore.targetWorkbook.Names.Add(rangeName, range);
                    }
                    else
                    {
                        return;
                    }
                }
                currentSE.fieldName = range.Name.NameLocal;
                currentSE.fieldValue = range.Value == null ? "" : range.Value.ToString();
                currentSE.valueAddress = range.Address;
                currentSE.locateType = NAME_TYPE;
                currentSE.sheetName = range.Worksheet.Name;
                currentSE.range1 = range;
                currentSE.range2 = range;
                AppStore.curOprateWindow.setTip(string.Format("设置命名： {0} ！", currentSE.fieldName, currentSE.fieldValue));
                AppStore.listEle.Add(currentSE);
                AppStore.curOprateWindow.SetCount(AppStore.listEle.Count);
            }
        }


        String calRelativePosition(SourceElement se)
        {
            var startRow = se.range1.Row;
            var startCol = se.range1.Column;

            var endRow = se.range2.Row;
            var endCol = se.range2.Column;

            var deltaRow = endRow - startRow;
            var deltaCol = endCol - startCol;

            string tag1 = deltaRow >= 0 ? "下" : "上";
            string tag2 = deltaCol >= 0 ? "右" : "左";

            if (deltaCol == 0 && deltaRow == 0) return "重合";

            var str1 = deltaRow == 0 ? "" : tag1 + Math.Abs(deltaRow).ToString();
            var str2 = deltaCol == 0 ? "" : tag2 + Math.Abs(deltaCol).ToString();

            return str1 + str2;
        }




    }
}
